import { useRequest } from 'ahooks';
import { useEffect, useMemo } from 'react';
import { API_URL } from '../../_setters/config';
import { Request } from '../../../src/components/utils/request';


export async function getRelationInfoByTableId(id: string) {
  return Request.get<any>(API_URL + `/relation/info/${id}`);
}

export const useRelationTableInfo = (relationTableId: string) => {
  const { data, run } = useRequest((id) => getRelationInfoByTableId(id), {
    manual: true,
  });

  useEffect(() => {
    if (relationTableId) {
      run(relationTableId);
    }
  }, [relationTableId]);

  const options = useMemo(() => {
    return (
      data?.map((item) => {
        return {
          label: `${item?.resourceEntity?.title} (被 ${item?.columnEntity?.title} 管理）`,
          value: item.tableId,
        };
      }) || []
    );
  }, [data]);

  return { options };
};

//
